package ui;

import bot.Bot;
import bot.ClientLoader;
import bot.ScriptMethods;
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class MainUI extends JFrame implements ActionListener {

	private Bot bot;
	private ClientLoader cl;
	private ScriptMethods scriptMethods;
	private ScriptUI scriptUI;
	public JPanel theGame;
	private String version = "2.1";

	public MainUI() {

		try {
			this.cl = new ClientLoader();
			this.scriptMethods = new ScriptMethods();
			this.bot = new Bot();
			this.scriptUI = new ScriptUI();
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		setVisible(false);

		theGame = new JPanel(new BorderLayout());
		theGame.setPreferredSize(new Dimension(512, 345));
		//if (read().equals(version)) {
			theGame.add(cl.applet, BorderLayout.CENTER);
		//} else {
		//	JOptionPane.showMessageDialog(null, "Your bot is outdated, grab the latest from arsbot.com.", "Bot outdated", JOptionPane.PLAIN_MESSAGE);
		//}
		super.setTitle("ARS 2.0");
		super.setDefaultCloseOperation(EXIT_ON_CLOSE);
		super.setSize(512, 345);
		super.getContentPane().add(theGame);

		try {
			UIManager.setLookAndFeel("org.jvnet.substance.skin.SubstanceRavenGraphiteLookAndFeel");
		} catch (Exception e) {
			System.out.println("Could not load skin. Using default.");
		}

		setResizable(false);
		setLocationRelativeTo(null);

		JMenuBar botMenu = new JMenuBar();
		JMenu menu = new JMenu("File");
		botMenu.add(menu);
		JMenuItem menuItemX = new JMenuItem("Exit");
		menu.add(menuItemX);
		menuItemX.addActionListener(this);
		JMenu menu2 = new JMenu("Script");
		botMenu.add(menu2);
		JMenuItem menuItemR = new JMenuItem("Run script...");
		menu2.add(menuItemR);
		menuItemR.addActionListener(this);
		JMenuItem menuItemST = new JMenuItem("Stop script");
		menu2.add(menuItemST);
		menuItemST.addActionListener(this);
		JMenuItem menuItemNP = new JMenuItem("View NPCs");
		menu2.add(menuItemNP);
		menuItemNP.addActionListener(this);
		JMenu menu3 = new JMenu("Server");
		botMenu.add(menu3);
		JMenuItem world1 = new JMenuItem("World 1");
		world1.addActionListener(this);
		menu3.add(world1);
		JMenuItem world2 = new JMenuItem("World 2");
		world2.addActionListener(this);
		menu3.add(world2);
		JMenuItem world3 = new JMenuItem("World 3");
		world3.addActionListener(this);
		menu3.add(world3);
		JMenu menu4 = new JMenu("Help");
		botMenu.add(menu4);
		JMenuItem menuItemD = new JMenuItem("Help");
		menu4.add(menuItemD);
		menuItemD.addActionListener(this);
		JMenuItem menuItemW = new JMenuItem("Credits");
		menu4.add(menuItemW);
		menuItemW.addActionListener(this);
		JMenuItem menuItemA = new JMenuItem("IDs");
		menu4.add(menuItemA);
		menuItemA.addActionListener(this);
		JMenuItem menuItem6 = new JMenuItem("Methods");
		menu4.add(menuItem6);
		menuItem6.addActionListener(this);
		botMenu.add(Box.createHorizontalGlue());
		JButton jButton1 = new JButton("Screenshot");
		botMenu.add(jButton1);
		jButton1.addActionListener(this);
		JButton jButton2 = new JButton("View HPs");
		botMenu.add(jButton2);
		jButton2.addActionListener(this);
		setJMenuBar(botMenu);

		pack();
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand() == "Exit") {
			System.exit(1);
		} else if (e.getActionCommand() == "Help") {
			openURL("http://rscc.dyndns.info/rsc/guide.htm");
		} else if (e.getActionCommand() == "Credits") {
			openURL("http://rscc.dyndns.info/rsc/credits.htm");
		} else if (e.getActionCommand() == "IDs") {
			openURL("http://rscc.dyndns.info/rsc/ids.htm");
		} else if (e.getActionCommand() == "Methods") {
			openURL("http://rscc.dyndns.info/rsc/scriptmethods.htm");
		} else if (e.getActionCommand() == "View HPs") {
			new InfoUI();
		} else if (e.getActionCommand() == "View NPCs") {
			new NpcUI();
		} else if (e.getActionCommand() == "Screenshot") {
			try {
				scriptMethods.debug("Taking screenshot.");
				takeScreenshot(this);
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else if (e.getActionCommand() == "Run script...") {
			if (!scriptUI.hasStarted)
				scriptUI.initComponents();
			scriptUI.setVisible(true);
		} else if (e.getActionCommand() == "Stop script") {
			scriptUI.stopScript();
		}

		if (e.getActionCommand() == "World 1") {
			try {
				if (scriptMethods.isLoggedIn())
					scriptMethods.debug("Set world to 1.");
				scriptMethods.setWorld(1);
			} catch (Exception rl) {
				rl.printStackTrace();
			}
		} else if (e.getActionCommand() == "World 2") {
			try {
				if (scriptMethods.isLoggedIn())
					scriptMethods.debug("Set world to 2.");
				scriptMethods.setWorld(2);
			} catch (Exception rl2) {
				rl2.printStackTrace();
			}
		} else if (e.getActionCommand() == "World 3") {
			try {
				if (scriptMethods.isLoggedIn())
					scriptMethods.debug("Set world to 3.");
				scriptMethods.setWorld(3);
			} catch (Exception rl3) {
				rl3.printStackTrace();
			}
		}
	}

	public void openURL(String url) {
		java.awt.Desktop desktop = java.awt.Desktop.getDesktop();
		if (!desktop.isSupported(java.awt.Desktop.Action.BROWSE)) {
			System.out.println("Could not open URL - your desktop is not supported.");
		} else {
			try {
				java.net.URI uri = new java.net.URI(url);
				desktop.browse(uri);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private String read() {
		try {
			URL url = new URL("http://rscc.dyndns.info/rsc/version.txt");
			BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
			String str = in.readLine().trim();
			in.close();
			return str;
		} catch (Exception e) {
			System.out.println("Error");
			return "";
		}
	}

	private static DateFormat dateFormat = new SimpleDateFormat(
			"yyyyMMddHHmmssSSS");

	public void takeScreenshot(MainUI ui) throws AWTException, IOException {

		Date timeTaken = new Date();
		File dir = new File("./screenshots/");
		if (dir.exists()) {
			BufferedImage image = new Robot()
					.createScreenCapture(new Rectangle(ui.getX(), ui.getY(), ui
							.getWidth(), ui.getHeight()));
			ImageIO.write(image, "png",
					new File("./screenshots/" + dateFormat.format(timeTaken)
							+ ".png"));
		} else {
			boolean makeDir = dir.mkdir();
			BufferedImage image = new Robot()
					.createScreenCapture(new Rectangle(ui.getX(), ui.getY(), ui
							.getWidth(), ui.getHeight()));
			ImageIO.write(image, "png",
					new File("./screenshots/" + dateFormat.format(timeTaken)
							+ ".png"));
		}
	}

	public static void main(String[] args) {
		try {
			new MainUI();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}